package com.tianqu.tms.www.rental.controller;

import java.sql.Timestamp;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.RentalContractInfo;
import com.tianqu.tms.entity.RentalLocationInfo;
import com.tianqu.tms.www.rental.viewhelper.RentalContractAddInputViewHelper;

/**
 * 合同查询/修改 - 合同作废 controller
 * @author mufeiyu
 */
public class RentalContractInvalidConfirmController extends TmsController {
	private Logger logger = Logger.getLogger(RentalContractInvalidConfirmController.class);
	RentalContractAddInputViewHelper contractSerch = new RentalContractAddInputViewHelper();

	@SuppressWarnings("unchecked")
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			RentalContractAddInputViewHelper view = (RentalContractAddInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);

			RentalContractInfo contractInfo = RentalContractInfo.getInstance();			
			TmsUserProfile profile = uc.getUserProfile();			
			String updataTime = DateUtil.getCurrentTime();
			contractInfo.setStatus(4);
			contractInfo.setId(view.getId());
			contractInfo.setSerial(view.getSerial());
			contractInfo.setUpdateUserId(profile.getUniqueId());		

			//更新合同状态
			contractInfo.updateContractStatus(contractInfo);
		
			// 插入 租赁合同操作记录表
			contractInfo.setHandleType(3);//3：合同作废
			contractInfo.setRemark(uc.getParameter("remark"));
			contractInfo.insertContractUpdateHistroy(contractInfo);
			
			logger.debug(this.getClass().getName() + "#execute was completed.");
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}
}
